package com.js.sys.adapter;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.js.lib_common.mvp.model.response.BindBean;
import com.js.lib_common.mvp.model.response.MachineIdInfoResponse;
import com.js.lib_common.mvp.model.response.MaterialIdInfoResponse;
import com.js.sys.R;

import java.util.List;

/**
 * 扫码单adapter
 */
public class BindingAdapter extends BaseMultiItemQuickAdapter<BindBean, BaseViewHolder> {

    private Context context;


    /**
     * Same as QuickAdapter#QuickAdapter(Context,int) but with
     * some initialization data.
     *
     * @param data A new list is created out of this one to avoid mutable list
     */
    public BindingAdapter(Context context, List<BindBean> data) {
        super(data);
        this.context = context;
        addItemType(BindBean.REQUEST_CODE_BIND_MACHINEID, R.layout.item_bind_machineid);//录入，整机码
        addItemType(BindBean.REQUEST_CODE_BIND_MATERIALID, R.layout.item_bind_matrrialid);//录入，物料码
//        addItemType(BindBean.REQUEST_CODE_BIND_PROCESS, R.layout.item_bind_process);//录入，工序
//        addItemType(BindBean.REQUEST_CODE_DEFAULT_WORK, R.layout.item_bind_default_work);//录入，默认工序
//        addItemType(BindBean.REQUEST_CODE_CHECK, R.layout.item_check);//检测，整机码
        addItemType(BindBean.REQUEST_CODE_CHECK_MACHINEID, R.layout.item_check_machineid);//检测，整机码
        addItemType(BindBean.REQUEST_CODE_CHECK_MATERIALID, R.layout.item_check_matrrialid);//检测，物料码

        addItemType(BindBean.REQUEST_CODE_CHECK_IN, R.layout.item_check_in);//入库，整机码
        addItemType(BindBean.REQUEST_CODE_CHECK_OUT, R.layout.item_check_out);//出库，整机码
        addItemType(BindBean.REQUEST_CODE_CHECK_OUT_CUSTOM, R.layout.item_check_out_custom);//出库，客户定制码
    }


    @Override
    protected void convert(BaseViewHolder helper, BindBean bean) {
        switch (helper.getItemViewType()) {
            case BindBean.REQUEST_CODE_BIND_MACHINEID:
                case_REQUEST_CODE_BIND_MACHINEID(helper, bean);
                break;
            case BindBean.REQUEST_CODE_BIND_MATERIALID:
                case_REQUEST_CODE_BIND_MATERIALID(helper, bean);
                break;
//            case BindBean.REQUEST_CODE_BIND_PROCESS:
//                case_REQUEST_CODE_BIND_PROCESS(BindBean.REQUEST_CODE_BIND_PROCESS, helper, bean);
//                break;
//            case BindBean.REQUEST_CODE_DEFAULT_WORK:
//                case_REQUEST_CODE_BIND_PROCESS(BindBean.REQUEST_CODE_DEFAULT_WORK, helper, bean);
//                break;
            case BindBean.REQUEST_CODE_CHECK_MACHINEID:
                REQUEST_CODE_CHECK(BindBean.REQUEST_CODE_CHECK_MACHINEID, helper, bean);
                break;
            case BindBean.REQUEST_CODE_CHECK_MATERIALID:
                REQUEST_CODE_CHECK(BindBean.REQUEST_CODE_CHECK_MATERIALID, helper, bean);
                break;
            case BindBean.REQUEST_CODE_CHECK_IN:
                case_REQUEST_CODE_CHECK_IN(helper,bean);
                break;
            case BindBean.REQUEST_CODE_CHECK_OUT:
                case_REQUEST_CODE_CHECK_OUT(helper,bean);
                break;
            case BindBean.REQUEST_CODE_CHECK_OUT_CUSTOM:
                case_REQUEST_CODE_CHECK_OUT_CUSTOM(helper,bean);
                break;
        }
        ImageView view= helper.getView(R.id.im_del);
        view.setVisibility(isShowDel?View.VISIBLE:View.GONE);

    }
    private boolean isShowDel=true;
        public  void notShowDel(){
            isShowDel=false;
        }
    /**
     * 绑定整机码
     */
    private void case_REQUEST_CODE_BIND_MACHINEID(BaseViewHolder helper, BindBean bean) {
        MachineIdInfoResponse mMachineIdInfo = bean.getMachineIdInfo();
        if (mMachineIdInfo == null) {
            return;
        }
        helper.setText(R.id.tv_code_type, "整机码");
        helper.setText(R.id.tv_device_serialCode, mMachineIdInfo.getBaseInfo().getSerialCode());
        helper.setText(R.id.tv_device_product, mMachineIdInfo.getBaseInfo().getProductMaterialCode());
        helper.setText(R.id.tv_device_name, mMachineIdInfo.getMachineMsg().getProductName());

        int Type_CheckIn = 1;//已入库
        int Type_CheckOut = 2;//已出库
        if (mMachineIdInfo.getBaseInfo().getStatus() == Type_CheckIn) {
            helper.setText(R.id.tv_device_checkin_type, "已入库");
        } else if (mMachineIdInfo.getBaseInfo().getStatus() == Type_CheckOut) {
            helper.setText(R.id.tv_device_checkin_type, "已出库");
        } else {
            helper.setText(R.id.tv_device_checkin_type, "未入库");
        }
        helper.addOnClickListener(R.id.im_del);
    }

    /**
     * 绑定物料码
     */
    private void case_REQUEST_CODE_BIND_MATERIALID(BaseViewHolder helper, BindBean bean) {
        MaterialIdInfoResponse mMaterialIdInfo = bean.getMaterialIdInfo();
        if (mMaterialIdInfo == null) {
            return;
        }
        helper.setText(R.id.tv_code_type, "物料码");
        helper.setText(R.id.tv_material_serialCode, mMaterialIdInfo.getBaseInfo().getSerialCode());
        helper.setText(R.id.tv_material_materialCode, mMaterialIdInfo.getBaseInfo().getMaterialCode());
        helper.setText(R.id.tv_material_status, mMaterialIdInfo.getBaseInfo().getStatus()==2?"存在绑定记录":"不存在绑定记录");

        helper.setText(R.id.tv_material_materialName, mMaterialIdInfo.getMaterialMsg().getMaterialName());
     LinearLayout ll= helper.getView(R.id.ll_bind);
        if (bean.getMaterialIdInfo().getCurrentBindingMachineId()!=null&&
                !bean.getMaterialIdInfo().getCurrentBindingMachineId().isEmpty()) {
            ll.setVisibility(View.VISIBLE);

            helper.setText(R.id.tv_material_bind, bean.getMaterialIdInfo().getCurrentBindingMachineId());
        }else {
            ll.setVisibility(View.GONE);
        }





        helper.addOnClickListener(R.id.im_del);

    }

//    /**
//     * 绑定工序码
//     */
//    private void case_REQUEST_CODE_BIND_PROCESS(int type, BaseViewHolder helper, BindBean bean) {
//        ProcessInfoInfoResponse mProcessInfoInfo = bean.getProcessInfoInfo();
//        if (mProcessInfoInfo == null) {
//            return;
//        }
//        ProcessInfoInfoResponse.ProcessInfoBean mProcessInfoBean = mProcessInfoInfo.getProcessInfo();
//        if (mProcessInfoBean != null) {
//            helper.setText(R.id.tv_process_code, mProcessInfoBean.getProcessCode());
//            helper.setText(R.id.tv_process_productCode, mProcessInfoBean.getProductCode());
//            helper.setText(R.id.tv_process_name, mProcessInfoBean.getProcessName());
//        }
//        if (type == BindBean.REQUEST_CODE_BIND_PROCESS) {
//            helper.setText(R.id.tv_code_type, "工序码");
//        } else {
//            helper.setText(R.id.tv_code_type, "工序码(设置的默认工序)");
//        }
//
//        helper.addOnClickListener(R.id.im_del);
//        List<String> materialCodeList = mProcessInfoInfo.getProcessMaterialCodeList();
//        StringBuilder stringBuilder = new StringBuilder();
//        if (materialCodeList.size() == 0) {
//            stringBuilder.append("暂无数据");
//        } else {
//            for (String s : materialCodeList) {
//                stringBuilder.append(s).append("\n");
//            }
//            stringBuilder.delete(stringBuilder.length() - 1, stringBuilder.length());
//        }
//        helper.setText(R.id.tv_process_list, stringBuilder.toString());
//    }


    /**
     * 检测
     */
    private void REQUEST_CODE_CHECK(int type, BaseViewHolder helper, BindBean bean) {
        RadioGroup rbtn = helper.getView(R.id.radioGroup);
        RadioButton rbtnok = helper.getView(R.id.rbtn_yes);
        rbtnok.setChecked(true);
        rbtn.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                switch (radioGroup.getCheckedRadioButtonId()) {
                    case R.id.rbtn_yes:
                        bean.setStandard(true);
                        break;
                    case R.id.rbtn_no:
                        bean.setStandard(false);
                        break;
                }
            }
        });
        EditText edtInput = helper.getView(R.id.tv_input);
        edtInput.setText("");
        edtInput.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                bean.setInfo(charSequence.toString());
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
        if (type == BindBean.REQUEST_CODE_CHECK_MACHINEID) {
            case_REQUEST_CODE_BIND_MACHINEID(helper, bean);
        }
        if (type == BindBean.REQUEST_CODE_CHECK_MATERIALID) {
            case_REQUEST_CODE_BIND_MATERIALID(helper, bean);
        }
    }
    /**
     * 入库
     */
    private void case_REQUEST_CODE_CHECK_IN(BaseViewHolder helper, BindBean bean) {
        helper.setText(R.id.tv_code_type, "入库整机码");
        helper.setText(R.id.tv_checkin_serialCode, bean.getQrcode());
        helper.addOnClickListener(R.id.im_del);
    }

    /**
     * 出库,本厂
     */
    private void case_REQUEST_CODE_CHECK_OUT(BaseViewHolder helper, BindBean bean) {
        helper.setText(R.id.tv_code_type, "出库整机码");
        helper.setText(R.id.tv_checkout_serialCode, bean.getQrcode());
        helper.addOnClickListener(R.id.im_del);
    }

    /**
     * 出库，客户定制
     */
    private void case_REQUEST_CODE_CHECK_OUT_CUSTOM(BaseViewHolder helper, BindBean bean) {
        helper.setText(R.id.tv_code_type, "客户定制出库整机码");
        helper.setText(R.id.tv_custom_serialCode, bean.getQrcode());
        helper.addOnClickListener(R.id.im_del);
    }
}
